package com.goscom.app.deeset;

import java.util.Calendar;

import com.goscom.app.database.DBAdapter;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class PersonalImformation extends Activity implements OnClickListener,
		OnFocusChangeListener {

	private String strMerId = "";
	private DBAdapter mDB;
	private EditText edtTelNum, edtEmail, edtMobNum, edtNation, edtBirth,
			edtInsur, edtStrdate, edtJobtit;
	private CheckBox ckbCon, ckbQuesA, ckbQuesB, ckbQuesC, ckbQuesD;
	private DatePickerDialog mDatePickerBirth, mDatePickerStart;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.personal_information);

		edtTelNum = (EditText) findViewById(R.id.edtPerInfoTelNum);
		edtEmail = (EditText) findViewById(R.id.edtPerInfoEmail);
		edtMobNum = (EditText) findViewById(R.id.edtPerInfoMobNum);
		edtNation = (EditText) findViewById(R.id.edtPerInfoNation);
		edtBirth = (EditText) findViewById(R.id.edtPerInfoBirth);
		edtInsur = (EditText) findViewById(R.id.edtPerInfoInsur);
		edtStrdate = (EditText) findViewById(R.id.edtPerInfoStartDate);
		edtJobtit = (EditText) findViewById(R.id.edtPerInfoJobTit);
		ckbCon = (CheckBox) findViewById(R.id.ckbPerInfoControl);
		ckbQuesA = (CheckBox) findViewById(R.id.ckbPerInfoQuesA);
		ckbQuesB = (CheckBox) findViewById(R.id.ckbPerInfoQuesB);
		ckbQuesC = (CheckBox) findViewById(R.id.ckbPerInfoQuesC);
		ckbQuesD = (CheckBox) findViewById(R.id.ckbPerInfoQuesD);

		mDB = new DBAdapter(PersonalImformation.this);
		mDB.open();

		getMerId();
		edtBirth.setOnClickListener(this);
		edtStrdate.setOnClickListener(this);
		edtBirth.setOnFocusChangeListener(this);
		edtStrdate.setOnFocusChangeListener(this);
	}

	public void getMerId() {
		strMerId = getIntent().getStringExtra("merid");
		ContentValues content = new ContentValues();
		content = mDB.queryDataByMerId("PERINFO", strMerId);
		if (content != null) {
			edtTelNum.setText(content.getAsString("telnum"));
			edtEmail.setText(content.getAsString("email"));
			edtMobNum.setText(content.getAsString("mobnum"));
			edtNation.setText(content.getAsString("nation"));
			edtBirth.setText(content.getAsString("birth"));
			edtInsur.setText(content.getAsString("insur"));
			edtStrdate.setText(content.getAsString("strdate"));
			edtJobtit.setText(content.getAsString("jobtit"));
			if (content.getAsString("control").equals("1")) {
				ckbCon.setChecked(true);
			}
			if (content.getAsString("ckcA").equals("1")) {
				ckbQuesA.setChecked(true);
			}
			if (content.getAsString("ckcB").equals("1")) {
				ckbQuesB.setChecked(true);
			}
			if (content.getAsString("ckcC").equals("1")) {
				ckbQuesC.setChecked(true);
			}
			if (content.getAsString("ckcD").equals("1")) {
				ckbQuesD.setChecked(true);
			}
		} else {
			edtBirth.setText(new StringBuilder()
					.append(Calendar.getInstance().getTime().getDate())
					.append("/")
					.append(Calendar.getInstance().getTime().getMonth() + 1)
					.append("/")
					.append(Calendar.getInstance().getTime().getYear() + 1900));
			edtStrdate.setText(new StringBuilder()
					.append(Calendar.getInstance().getTime().getDate())
					.append("/")
					.append(Calendar.getInstance().getTime().getMonth() + 1)
					.append("/")
					.append(Calendar.getInstance().getTime().getYear() + 1900));
		}
	}

	public void Logout(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(PersonalImformation.this, Login.class));
	}

	public void goPrevious(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(PersonalImformation.this,
				MerchandiserName.class).putExtra("merid", strMerId));
	}

	public void goNext(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(PersonalImformation.this, Address.class)
				.putExtra("merid", strMerId));
	}

	public void Save(View v) {
		saveDB();
		Toast.makeText(PersonalImformation.this, "Save Successful!",
				Toast.LENGTH_LONG).show();
	}

	public void saveDB() {
		if (mDB.queryMerchandiser("PERINFO", strMerId) == 0) {
			insertDB();
		} else {
			mDB.deleteData("PERINFO", strMerId);
			insertDB();
		}
	}

	public void insertDB() {
		ContentValues content = new ContentValues();
		content.put("merid", strMerId);
		content.put("telnum", edtTelNum.getText().toString());
		content.put("control", ckbCon.isChecked());
		content.put("email", edtEmail.getText().toString());
		content.put("mobnum", edtMobNum.getText().toString());
		content.put("nation", edtNation.getText().toString());
		content.put("birth", edtBirth.getText().toString());
		content.put("insur", edtInsur.getText().toString());
		content.put("strdate", edtStrdate.getText().toString());
		content.put("jobtit", edtJobtit.getText().toString());
		content.put("ckcA", ckbQuesA.isChecked());
		content.put("ckcB", ckbQuesB.isChecked());
		content.put("ckcC", ckbQuesC.isChecked());
		content.put("ckcD", ckbQuesD.isChecked());
		mDB.insertData("PERINFO", content);
	}

	@Override
	protected void onRestart() {
		mDB.open();
		super.onRestart();
	}

	@Override
	protected void onDestroy() {
		if (mDB != null) {
			mDB.close();
		}
		super.onDestroy();
	}

	private DatePickerDialog.OnDateSetListener mDateBirthListener = new DatePickerDialog.OnDateSetListener() {

		// when dialog box is closed, below method will be called.
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {
			edtBirth.setText(new StringBuilder().append(selectedDay)
					.append("/").append(selectedMonth + 1).append("/")
					.append(selectedYear));
		}
	};

	private DatePickerDialog.OnDateSetListener mDateStartListener = new DatePickerDialog.OnDateSetListener() {

		// when dialog box is closed, below method will be called.
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {
			edtStrdate.setText(new StringBuilder().append(selectedDay)
					.append("/").append(selectedMonth + 1).append("/")
					.append(selectedYear));
		}
	};

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		switch (v.getId()) {
		case R.id.edtPerInfoBirth:
			if (hasFocus) {
				mDatePickerBirth = new DatePickerDialog(this,
						mDateBirthListener, Calendar.getInstance().get(
								Calendar.YEAR), Calendar.getInstance().get(
								Calendar.MONTH), Calendar.getInstance().get(
								Calendar.DAY_OF_MONTH));
				mDatePickerBirth.show();
			} else {

			}
			break;
		case R.id.edtPerInfoStartDate:
			if (hasFocus) {
				mDatePickerStart = new DatePickerDialog(this,
						mDateStartListener, Calendar.getInstance().get(
								Calendar.YEAR), Calendar.getInstance().get(
								Calendar.MONTH), Calendar.getInstance().get(
								Calendar.DAY_OF_MONTH));
				mDatePickerStart.show();
			} else {

			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.edtPerInfoBirth:
			mDatePickerBirth = new DatePickerDialog(this, mDateBirthListener,
					Calendar.getInstance().get(Calendar.YEAR), Calendar
							.getInstance().get(Calendar.MONTH), Calendar
							.getInstance().get(Calendar.DAY_OF_MONTH));
			mDatePickerBirth.show();
			break;
		case R.id.edtPerInfoStartDate:
			mDatePickerStart = new DatePickerDialog(this, mDateStartListener,
					Calendar.getInstance().get(Calendar.YEAR), Calendar
							.getInstance().get(Calendar.MONTH), Calendar
							.getInstance().get(Calendar.DAY_OF_MONTH));
			mDatePickerStart.show();
			break;
		default:
			break;
		}
	}
}
